package org.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.sys.services.MenuService;
import org.sys.vo.Menu;
import org.sys.vo.User;

@Controller
@RequestMapping("/sys")
public class MenuController {
	
	private static Logger log = LoggerFactory.getLogger(MenuController.class);
	
	@Autowired
	private MenuService menuService;

	/**
	 * 返回控制台
	 * @param request
	 * @return
	 */
	@RequestMapping("/systemController")
	public ModelAndView systemController(HttpServletRequest request){
		String userId = (String)request.getSession().getAttribute("userId");
		log.debug(userId+"\taccess: systemController");
		
		List<Menu> userMenu = menuService.getAllMenu(userId);
		Map<String,List<Menu>> map = new HashMap<String,List<Menu>>();
		map.put("menu", userMenu);
		ModelAndView view = new ModelAndView("/jsp/main",map);
		return view;
	}
	
	/**
	 * 用户所有菜单
	 * @param request
	 * @return
	 */
	@RequestMapping("/getMenu")
	@ResponseBody
	public List<Menu> userController(HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("user");
		log.debug(user.getUserId()+"\t access: userController");
		return menuService.getAllMenu(user.getUserId());
	}
}
